#include <Servo.h>

int targets [4];
int positions [4];
const uint8_t inputs [4] = { A0, A1, A2, A3 };

Servo servos [4];

void setup()
{
  for (int i = 0; i < 4; ++i)
  {
    servos[i].attach(i + 2);
  }
  Serial.begin(115200);
  delay(3000);
}

bool movementComplete()
{
  return positions[0] == targets[0] &&
  positions[1] == targets[1] &&
  positions[2] == targets[2] &&
  positions[3] == targets[3];
}

void loop()
{
  for (int i = 0; i < 4; ++i)
  {
    int input = analogRead(inputs[i]);
    input = map(analogRead(inputs[i]), 0, 900, 0, 180);
    if (input > targets[i] + 1 || input < targets[i] - 1)
    {
      targets[i] = input;
    }
  }

  while (!movementComplete())
  {
    for (int i = 0; i < 4; ++i)
    {
      int t = targets[i];
      int p = positions[i];
      p = p < t ? p + 1 : p > t ? p - 1 : p;
      servos[i].write(p);
      positions[i] = p;
    }
    
    for (int i = 0; i < 3; ++i)
    {
      Serial.print(positions[i]);
      Serial.print(':');
    }
    
    Serial.print(positions[3]);
    
    delay(25);
  }
}
